/*
 * surfacehigh.js
 * 天气形势
 */
;
(function (call) {
	var main = {
		baseUrl: CafeJS.CXT,
		mapId: "tjMap",
		playList: null,
		datepicker: null,
		legendpicker: null,
		latestTime: null,
		router: {
			surfaceHighData: 'zhfx/weatherter/getQysttData',
			productcfg: "/business/common/json/product.json",
			squarelegendCfg: "/business/common/json/weatherterrain.json"
		},
		init: function () {
			this.initView();
			this.bindEvt();
		},
		initView: function () {
			var that = this;
			that.initDatePicker();
			$.getJSON(that.router.productcfg, function (cfg) {
				that.initProductList(cfg);
			})
		},
		// 初始化产品列表
		initProductList: function (cfg) {
			var that = this
			var params = {
				container: '.product-content.surfacehigh',
				data: cfg.surfacehigh,
				eleClick: function (ele, oldEle, selected) {
					var name = ele.ele_name;
					var pro = {
						name: name,
						time: that.calcStartTime(ele),
						bussitype: 'surfacehigh',
						ele: ele,
						isGrid: true,
						isSK: true
					}
					if (selected) {
						that.clearWeathertrrain();
						if (ele.type == 'high') {
							$(".surfacehigh .product-list .product-title").first().next().find('li.active').click();
						} else {
							$(".surfacehigh .product-list .product-title").last().next().find('li.active').click();
						}
						// 增加产品图层
						that.addLayerManage(pro);
						that.queryEleData(ele, pro.time);
					} else {
						that.clearWeathertrrain(ele.type);
						CafeJS.layerManage.removeLayer(pro); // 删除当前要素的图层
					}
				}
			};
			CafeJS.analysis(params);
            var hour = new Date(that.datepicker.getVal()).getHours();
            if (hour == 8 || hour == 20) {
                that.preventEventByHours(false);
            } else {
                that.preventEventByHours(true);
            }
		},
		// 初始化时间控件
		initDatePicker: function () {
			var _this = this;
			this.datepicker = CafeJS.Datepicker({
				appendTo: "#datepicker-surfacehigh",
				curDate: _this.latestTime,
				type: 3,
				dateFmt: 'yyyy-MM-dd hh',
				dateType: 'H',
				timeInterval: 3,
				customHm: [2, 5, 8, 11, 14, 17, 20],
				call: function (val) {
					_this.queryMultiEleData();
					var hour = new Date(_this.datepicker.getVal()).getHours();
					if (hour == 8 || hour == 20) {
						_this.preventEventByHours(false);
					} else {
						_this.preventEventByHours(true);
					}
				},
				updateDateCall: function () {
					var hour = new Date(_this.datepicker.getVal()).getHours();
					if (hour == 8 || hour == 20) {
						_this.preventEventByHours(false);
					} else {
						_this.preventEventByHours(true);
					}
				}
			});
			$("#datepicker-surfacehigh").show();
		},
		//当分为00时，元素不能点击，置灰
		preventEventByHours: function (flag) {
			debugger
			$(".surfacehigh .product-list li").each(function (index) {
				var $obj = $(this);
				var interval = $obj.data("pro").interval;
				if (interval == 720) {
					if (flag) {
                        if ($obj.hasClass("active")) {
                            $obj.click();
                        }
						$obj.addClass("preventEventLi");
						$obj.children().addClass("preventEvent");
					} else {
						$obj.removeClass("preventEventLi");
						$obj.children().removeClass("preventEvent");
						$obj.attr("title", "");
					}
				}
			})
		},
		// 初始化播放列表
		initPlayList: function (pro) {
			var that = this;
			var startTime = pro.time;

			var interval = pro.ele.interval || 5;
			this.playList = CafeJS.Timeline3({
				appendTo: "#a-m-timeline",
				curDate: startTime,
				type: 1,
				pro: pro,
				dateOpt: {
					dateFmt: 'yyyy-MM-dd hh:mm',
					timeInterval: interval,
					customHm: [2, 5, 8, 11, 14, 17, 20],
				},
				timeInterval: interval,
				layers: [],
				gisId: that.mapId,
				call: function (pro) {
					CafeJS.layerManage.updateLayer(pro);
					var ele = pro.ele;
					that.queryEleData(ele, pro.time);
				}
			});
		},
		// 查询已选择要素
		queryMultiEleData: function () {
			var that = this;
			var pro, time;
			$(".surfacehigh .product-list li.active").each(function (i, ele) {
				pro = $(ele).data('pro');
				time = that.calcStartTime(pro);
				CafeJS.layerManage.updateLayerTime(time, 'surfacehigh', null, pro.ele_id);
				that.clearWeathertrrain();
				that.queryEleData(pro, time);
			});
		},
		// 查询要素数据
		queryEleData: function (ele, time) {
			this.querySurfaceHigh(ele, time);
		},
		// 展示地面高空要素
		querySurfaceHigh: function (ele, time) {
			var that = this;
			var isHigh = ele.type == 'high' ? true : false;
			var layerId = 'weathertrrain-layer-' + ele.ele_id;
			if (this.squarelegend) {
				this.squarelegend.remove(isHigh ? "surface" : "high");
			}
			if (isHigh) {
				this.squarelegend = CafeJS.Squarelegend({
					appendTo: '.cafe-busis-squarelegend', //控件所在容器
					gisId: this.mapId,
					title: "高度全要素图例", //图例标题
					layerId: layerId,
					type: "high"
				});
			} else {
				this.squarelegend = CafeJS.Squarelegend({
					appendTo: '.cafe-busis-squarelegend', //控件所在容器
					gisId: this.mapId,
					title: "地面全要素图例", //图例标题
					layerId: layerId,
					type: "surface"
				});
			}
			var param = {
				queryTime: time,
				height: isHigh ? ele.ele_id : '',
				newFlag: true
			}
			$.getJSON(that.router.squarelegendCfg, function (eleConfigs) {
				$.getJSON(that.router.surfaceHighData, param, function (result) {
					if (!result.isSuccess) return;
					data = result.data;
					if (!data.STAT) {
						alert("该时间点没有数据");
						return;
					}
					var options = {
						id: layerId,
						GLMap: GLMap,
						data: data.STAT,
						statType: isHigh ? "high" : "ground",
						eleConfig: eleConfigs
					};
					GIS.showAllElements(options);
					var Tdata = null,
						HGTdata = null,
						SLPdata = null;
					if (isHigh && data) {
						Tdata = data['HIGH_T'];
						HGTdata = data['HIGH_HEIGHT'];
					} else {
						SLPdata = data.PRES_SLP;
					}

					var isolinesData = [];
					if (SLPdata) {
						isolinesData.push({
							color: "rgb(0, 0, 200)",
							data: SLPdata
						});
					}
					if (Tdata) {
						isolinesData.push({
							color: "rgb(60, 137, 241)",
							data: Tdata
						});
					}
					if (HGTdata) {
						isolinesData.push({
							color: "rgb(192, 0, 0)",
							data: HGTdata
						});
					}
					$.each(isolinesData, function (k) {
						var dataArrs = this.data;
						var lineData = [];
						var lineData_588 = []; //588线
						dataArrs.forEach(function (d) {
							if (d.first == '588') {
								lineData_588.push(d);
							} else {
								lineData.push(d);
							}
						})
						var lineDatas = [];
						var contourColors = [];
						var sizes = [];

						lineDatas.push(lineData);
						contourColors.push(this.color);
						sizes.push(1.5);
						//588线
						if (lineData_588.length > 0) {
							lineDatas.push(lineData_588);
							contourColors.push(this.color);
							sizes.push(2);
						}
						layerId = layerId + '-' + (k + 1);
						GIS.Layer.IsolineLayerHelper.createIsolineLayerForCave(GLMap, layerId, lineDatas, contourColors, sizes, 3000, GLMap.spatialReference.wkid);
					});
				})
			});
		},
		// 增加产品图层
		addLayerManage: function (pro) {
			var that = this;
			if (CafeJS.layerManage) {
				CafeJS.layerManage.addLayer(pro);
			} else {
				CafeJS.layerManage = CafeJS.LayerManage({
					appendTo: "#c-b-layermanage",
					mapId: that.mapId,
					proInfo: pro,
					call: function (pro) {
						CafeJS[pro.bussitype].initPlayList(pro);
					},
					more: function (pro) {
						CafeJS[pro.bussitype].stationView(pro);
					},
					close: function (pro) {
						CafeJS[pro.bussitype].delLayer(pro);
					}
				})
			}
		},
		// 根据时间间隔获取开始时间
		calcStartTime: function (ele) {
			var time = new Date(this.datepicker.getVal());
			switch (ele.interval) {
				case 10:
					time.setMinutes(Math.floor(time.getMinutes() / 10) * 10);
					break;
				case 60:
					time.setMinutes(0);
					break;
			}
			return time.format('yyyy-MM-dd hh:mm');
		},
		// 删除图层后取消要素选择
		delLayer: function (pro) {
			var eleid = pro.ele.ele_id;
			$(".surfacehigh .product-list li.active").each(function () {
				if ($(this).data('eleid') == eleid) {
					$(this).click();
					return false;
				}
			})
		},
		clearWeathertrrain: function (type) {
			var layerIds = [];
			GLMap.layerIds.forEach(function (layerId) {
				if (layerId.indexOf('weathertrrain-layer') != -1) {
					layerIds.push(layerId);
				}
			})
			if (layerIds.length > 0)
				GIS.LayerControll.deleteLayer(this.mapId, layerIds);

			if (type) {
				this.squarelegend.remove(type);
			} else if (this.squarelegend) {
				this.squarelegend.remove("surface");
				this.squarelegend.remove("high");
			}
		},
		bindEvt: function () {}
	};
	if (typeof call === 'function') {
		CafeJS.surfacehigh = main;
		call(main);
	}

})(function (main) {
	CafeJS.models(['datepicker', 'legentpicker', 'legentconfig', 'ajax', 'squarelegend', 'timeLine3'], function () {
		main.init();
	});
});